package cn.com.blueInfo.bpm.template.controller;

import cn.com.blueInfo.bpm.common.controller.BaseController;
import cn.com.blueInfo.bpm.common.validation.Create;
import cn.com.blueInfo.bpm.common.validation.Update;
import cn.com.blueInfo.bpm.common.validation.UpdatePartial;
import cn.com.blueInfo.bpm.template.request.InstanceDTO;
import cn.com.blueInfo.bpm.template.response.InstanceVO;
import cn.com.blueInfo.bpm.template.service.InstanceService;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;
import java.util.List;

/**
 * 流程运行时实例表 前端控制器
 * @author suxch
 * @since 2025-08-24 01:41:15
 */
@Tag(name = "流程运行时实例 接口")
@RestController
@RequestMapping("/template/instance")
public class InstanceController extends BaseController {

    @Autowired
    private InstanceService instanceService;

    /**
     * 新建流程运行时实例
     * @param instanceDTO 流程运行时实例参数
     * @return ResultInfo
     */
    @Operation(summary = "新增流程运行时实例")
    @PostMapping("/addInfo")
    public ResultInfo<Integer> createInstance(
            @Validated({Create.class, Default.class}) @RequestBody InstanceDTO instanceDTO) {
        return instanceService.addInstance(instanceDTO);
    }

    /**
     * 批量新建流程运行时实例
     * @param instanceDTOList 流程运行时实例参数列表
     * @return ResultInfo
     */
    @Operation(summary = "批量新建流程运行时实例")
    @PostMapping("/addInfoList")
    public ResultInfo<Integer> batchCreateInstance(
            @Validated({Create.class, Default.class}) @RequestBody List<InstanceDTO> instanceDTOList) {
        return instanceService.batchAddInstance(instanceDTOList);
    }

    /**
     * 根据ID删除流程运行时实例
     * @param instanceId 流程运行时实例ID
     * @return ResultInfo
     */
    @Operation(summary = "根据ID删除流程运行时实例")
    @DeleteMapping("/removeInfo/{instanceId}")
    public ResultInfo<Integer> deleteInstanceById(
            @Parameter(name = "ID") @PathVariable Long instanceId) {
        return instanceService.removeInstance(instanceId);
    }

    /**
     * 批量根据ID删除流程运行时实例
     * @param instanceIdList 流程运行时实例ID列表
     * @return ResultInfo
     */
    @Operation(summary = "批量根据ID删除流程运行时实例")
    @DeleteMapping("/removeInfoList/{instanceIdList}")
    public ResultInfo<Integer> batchDeleteInstanceById(
            @Parameter(name = "ID") @PathVariable List<Long> instanceIdList) {
        return instanceService.batchRemoveInstance(instanceIdList);
    }

    /**
     * 根据ID全量更新流程运行时实例
     * @param instanceId 流程运行时实例ID
     * @param instanceDTO 流程运行时实例参数
     * @return ResultInfo
     */
    @Operation(summary = "根据ID全量更新流程运行时实例")
    @PutMapping("/modifyInfo/{instanceId}")
    public ResultInfo<Integer> updateInstanceFull(
            @Parameter(name = "ID") @PathVariable Long instanceId,
            @Validated({Update.class, Default.class}) @RequestBody InstanceDTO instanceDTO) {
        validateUpdateId(instanceId, instanceDTO.getId());
        return instanceService.modifyInstance(instanceDTO);
    }

    /**
     * 根据ID部分更新流程运行时实例
     * @param instanceId 流程运行时实例ID
     * @param instanceUpdatePartialDTO 流程运行时实例参数
     * @return ResultInfo
     */
    @Operation(summary = "根据ID部分更新流程运行时实例")
    @PatchMapping("/modifyPartialInfo/{instanceId}")
    public ResultInfo<Integer> updateInstancePartial(
            @Parameter(name = "ID") @PathVariable Long instanceId,
            @Validated({UpdatePartial.class, Default.class}) @RequestBody
            InstanceDTO.UpdatePartialDTO instanceUpdatePartialDTO) {
        validateUpdateId(instanceId, instanceUpdatePartialDTO.getId());
        return instanceService.modifyInstancePartial(instanceUpdatePartialDTO);
    }

    /**
     * 根据ID查询流程运行时实例
     * @param instanceId 流程运行时实例ID
     * @return ResultInfo
     */
    @Operation(summary = "根据ID查询流程运行时实例")
    @GetMapping("/getInfo/{instanceId}")
    public ResultInfo<InstanceVO> getInstanceById(
            @Parameter(name = "ID") @PathVariable Long instanceId) {
        return instanceService.queryInstanceById(instanceId);
    }

    /**
     * 分页查询流程运行时实例
     * @param instanceQueryDTO 流程运行时实例参数
     * @return ResultInfo
     */
    @Operation(summary = "分页查询流程运行时实例")
    @GetMapping("/getInfos")
    public ResultInfo<Page<InstanceVO>> getInstancePage(
            @Validated InstanceDTO.QueryDTO instanceQueryDTO) {
        return instanceService.queryInstancePage(instanceQueryDTO);
    }

    /**
     * 根据条件查询流程运行时实例
     * @param instanceQueryDTO 流程运行时实参数
     * @return ResultInfo
     */
    @Operation(summary = "根据条件查询流程运行时实例")
    @GetMapping("/getInfoList")
    public ResultInfo<List<InstanceVO>> getInstanceList(
            @Validated InstanceDTO.QueryDTO instanceQueryDTO) {
        return instanceService.queryInstanceList(instanceQueryDTO);
    }

}
